import java.util.ArrayList;

import ij.ImagePlus;
import ij.ImageStack;
import ij.process.ImageProcessor;

/**
*
* Bachelor thesis at the Institute of Biomedical Engineering, 
* located at the UMIT/EWZ campus in Hall in Tyrol. <br><br>
*
* This is a helperclass with some usefull static methods to create
* deep copies of some datastructures
* 
* @author Stefan Fuchs
*
*/
public class Util {

	/**
	 * Returns a copy of this Stack
	 * 
	 * @param original
	 *            The original Stack to be copied
	 * @return
	 */
	public static ImageStack copyStack(ImageStack original) {
		ImageStack copy = new ImageStack(original.getWidth(), original
				.getHeight());

		for (int i = 1; i <= original.getSize(); i++) {
			copy.addSlice(original.getSliceLabel(i), original.getProcessor(i)
					.duplicate());
		}

		return copy;
	}
	
	/**
	 * Returns a copy of this VoxelList
	 * 
	 * @param original
	 * 			The original voxel list
	 * @return
	 * 			The copy
	 */
	public static ArrayList<Voxel> copyVoxelList(ArrayList<Voxel> original) {
		
		if ( original == null ) {
			return null;
		}
		
		ArrayList<Voxel> copy = new ArrayList<Voxel>();
		Voxel newVoxel;
		
		for(Voxel v : original) {
			newVoxel = new Voxel(v.getX(), v.getY(), v.getZ(), v.getValue());
			copy.add(newVoxel);
		}
		
		return copy;
	}
	
	/**
	 * Returns a copy of the 1D-array
	 * 
	 * @param original
	 * 			The original Array
	 * @return
	 * 			The copy
	 */
	public static int[] copy1DVoxelArry(int[] original) {
		int[] copy = new int[original.length];
		
		for (int i = 0; i < original.length; i++) {
			copy[i] = original[i];
		}
		
		return copy;
	}
	
	/**
	 * Returns an image with the current imagestack overlayed by the specified image
	 * 
	 * @param ip
	 * @return
	 */
	public static ImagePlus loadOverlay(ImagePlus ip) {
		
		int width = ip.getImageStack().getWidth();
		int height = ip.getImageStack().getHeight();
		int size = ip.getImageStack().getSize();
		
		ImagePlus retImage = new ImagePlus("OverlayImage",Modell.getInstance().getImageStack());
		ImageProcessor retImageProc = retImage.getProcessor();
		
		ImageProcessor iProc = ip.getProcessor();
		
		int[] array = new int[width * height * size];
		
		int coordinate1D;
		int[] voxel = null;
		for ( int z = 0 ; z < size ; z++) {
			
			ip.setSlice( z + 1 );
			retImage.setSlice( z + 1 );
			
			for ( int y = 0 ; y < height ; y++ ) {
				for ( int x = 0 ; x < width ; x++ ) {
					
					voxel = iProc.getPixel(x, y, voxel);
					
					coordinate1D = (int) ( width * ( height * z + y ) + x ) ;
					
					if ( voxel[0] == 1 ) {
						array[coordinate1D] = 1;
						retImageProc.putPixel(x, y, Modell.VENTRICLE_A_POINTS_LABEL.getRGB());
					} else if ( voxel[0] == 2 ) {
						array[coordinate1D] = 2;
						retImageProc.putPixel(x, y, Modell.VENTRICLE_B_POINTS_LABEL.getRGB());
					} else if (voxel[0] == 3 ) {
						array[coordinate1D] = 3;
						retImageProc.putPixel(x, y, Modell.OVERLAPPING_MYOCARD_LABEL.getRGB());
					} else if (voxel[0] == 4 ) {
						array[coordinate1D] = 4;
						retImageProc.putPixel(x, y, Modell.MANUALLY_CORRECTED_VOXELS.getRGB());
					} else {
						array[coordinate1D] = 0;
					}
					
				}
			}
		}
		
		Modell.getInstance().setMyocardiumLabel(Util.copy1DVoxelArry(array));
		
		return retImage;
	}
}
